package com.ruoyi.zq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.zq.enums.SubscriberType;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.zq.domin.ZqNoticeDetails;
import com.ruoyi.zq.domin.dto.ZqNoticeDTO;
import com.ruoyi.zq.domin.vo.NoticeDetailsVO;
import com.ruoyi.zq.mapper.ZqNoticeDetailsMapper;
import com.ruoyi.zq.service.ZqNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 *
 * @Author: LoneyFeather
 * @Date: Created in 15:25 2021/12/30
 */
@Service
@Slf4j
public class ZqNoticeServiceImpl extends ServiceImpl<ZqNoticeDetailsMapper, ZqNoticeDetails> implements ZqNoticeService {

    @Autowired
    private ZqNoticeDetailsMapper noticeDetailsMapper;

    @Autowired
    private ISysNoticeService sysNoticeService;

    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    @Autowired
    private ISysJobService sysJobService;

    @Override
    public List<NoticeDetailsVO> selectNoticeList(NoticeDetailsVO noticeVO) {
        noticeVO.setSubscriberUserId(SubscriberType.ALL.name()+"_"+SecurityUtils.getUserId());
        noticeVO.setSubscriberDeptId(SubscriberType.DEPT.name()+"_"+SecurityUtils.getDeptId());

        List<NoticeDetailsVO> list = noticeDetailsMapper.selectByDTO(noticeVO);
        return list;
    }

    @Override
    public NoticeDetailsVO selectNoticeById(Long noticeId) {
        return noticeDetailsMapper.selectByNoticeId(noticeId);
    }

    @Override
    public int addNoticeAndDetails(ZqNoticeDTO noticeDTO) {
         SysNotice notice = new SysNotice();
         notice = transitionNotice(noticeDTO);

        SysNotice notice1 = sysNoticeMapper.selectOne(new LambdaQueryWrapper<SysNotice>()
            .eq(SysNotice::getNoticeTitle,notice.getNoticeTitle()));
        if (ObjectUtils.isNotEmpty(notice1)){
            notice.setNoticeId(notice1.getNoticeId());
        }
         sysNoticeService.saveOrUpdate(notice);
         Long id = notice.getNoticeId();
         noticeDTO.setNoticeId(notice.getNoticeId());
         if (addDetails(noticeDTO)){
             return 1;
         }
        return 0;
    }

    @Override
    public int updateNotice(ZqNoticeDTO noticeDTO) {
//        SysNotice notice = transitionNotice(noticeDTO);
        SysNotice notice = new SysNotice();
        BeanUtils.copyProperties(noticeDTO,notice);
        sysNoticeMapper.updateNotice(notice);
        noticeDetailsMapper.delete(new LambdaQueryWrapper<ZqNoticeDetails>()
        .eq(ZqNoticeDetails::getNoticeId,noticeDTO.getNoticeId()));
        if (addDetails(noticeDTO)){
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        for (Long noticeId:noticeIds) {
            noticeDetailsMapper.delete(new LambdaQueryWrapper<ZqNoticeDetails>()
                    .eq(ZqNoticeDetails::getNoticeId,noticeId));
        }
        return sysNoticeMapper.deleteNoticeByIds(noticeIds);
    }

    @Override
    public ZqNoticeDTO getNoticePithOnTelephone(String activityName) {
        ZqNoticeDTO noticeDTO = new ZqNoticeDTO();
        noticeDTO.setNoticeTitle(activityName + "活动入选通知");
        noticeDTO.setNoticeContent("恭喜你入选此次志愿服务活动的志愿者名单，请按照要求参与此次活动，如有疑问请联系活动的发布者！！！");
        noticeDTO.setSubscriberType(SubscriberType.ALONE.name());
        noticeDTO.setStatus("0");
        noticeDTO.setNoticeType("1");
        noticeDTO.setCreateBy(SecurityUtils.getUsername());
        noticeDTO.setCreateTime(new Date());
        return noticeDTO;
    }


    /**
     * 保存ZqNoticeDetails接收对象数据
     * @param noticeDTO
     * @return
     */
    private Boolean addDetails(ZqNoticeDTO noticeDTO){
        List<ZqNoticeDetails> detailsList = new ArrayList<>();
        ZqNoticeDetails details = transitionDetails(noticeDTO);
        switch (noticeDTO.getSubscriberType()){
            case "ALL":
                details.setSubscriberId(SubscriberType.ALL.name());
                return saveOrUpdate(details);
            case "DEPT":
                details.setSubscriberId(SubscriberType.DEPT.name()+"_"+SecurityUtils.getDeptId());
                return saveOrUpdate(details);
            case "ALONE":
                //删除所有notice的具体详情，再新增
                noticeDetailsMapper.removeByNoticeId(noticeDTO.getNoticeId());
                if (ObjectUtils.isNotEmpty(noticeDTO.getUserIds())) {
                    for (Long userId : noticeDTO.getUserIds()) {
                        ZqNoticeDetails zqNoticeDetails = transitionDetails(noticeDTO);
                        zqNoticeDetails.setSubscriberId(SubscriberType.ALONE.name() + "_" + userId);

                        detailsList.add(zqNoticeDetails);
                    }
                    return saveOrUpdateBatch(detailsList);
                }
                return true;
            default:
                return false;
        }
    }

    /**
     * 查询数据库里面的数据。然后没有直接返回，有的话就赋值
     * @param details
     * @return
     */
    private ZqNoticeDetails updateDatabase(ZqNoticeDetails details){
        ZqNoticeDetails ob = noticeDetailsMapper.selectOne(new LambdaQueryWrapper<ZqNoticeDetails>()
        .eq(ZqNoticeDetails::getNoticeId,details.getNoticeId())
        .eq(ZqNoticeDetails::getSubscriberId,details.getSubscriberId()));
        if (ObjectUtils.isNotEmpty(ob)){
            BeanUtils.copyProperties(details,ob);
            return ob;
        }
        return details;
    }





    /**
     * 转换成SysNotice对象
     * @param noticeDTO
     * @return
     */
    private SysNotice transitionNotice(ZqNoticeDTO noticeDTO){
        SysNotice notice = new SysNotice();
        notice.setNoticeTitle(noticeDTO.getNoticeTitle());
        notice.setNoticeType(noticeDTO.getNoticeType());
        notice.setSubscriberType(noticeDTO.getSubscriberType());
        notice.setNoticeContent(noticeDTO.getNoticeContent());
        notice.setCreateBy(noticeDTO.getCreateBy());
        notice.setCreateTime(noticeDTO.getCreateTime());
        notice.setUpdateBy(noticeDTO.getUpdateBy());
        notice.setUpdateTime(noticeDTO.getUpdateTime());
        notice.setRemark(noticeDTO.getRemark());
        notice.setStatus(noticeDTO.getStatus());
        return notice;
    }

    /**
     * 转化成 ZqNoticeDetails对象
     * @param noticeDTO
     * @return
     */
    private ZqNoticeDetails transitionDetails(ZqNoticeDTO noticeDTO){
        ZqNoticeDetails details = new ZqNoticeDetails();
        details.setNoticeId(noticeDTO.getNoticeId());
        details.setIsDel(0);
        details.setIsRead(0);
        details.setCreateBy(noticeDTO.getCreateBy());
        details.setCreateTime(noticeDTO.getCreateTime());
        details.setUpdateBy(noticeDTO.getUpdateBy());
        details.setUpdateTime(noticeDTO.getUpdateTime());
        details.setRemark(noticeDTO.getRemark());
        return details;
    }

}
